<uieip type="page" value="login" debug="true" timeout="5000"></uieip>
<uieip type="reg" value="data,http"></uieip>
<script runat="server">
    var action = request.param("action");
    var dbName = "yqbase";
    var tbName = "stock_daily";
    var b_json = "{}";
    if (action == "dbs") {
        b_json = data.dbs();
    } else if (action == "title") {
        var tempno = request.param("tempno");
        b_json = data.title(dbName, tbName, tempno);
    } else if (action == "model") {
        var tempno = request.param("tempno");
        b_json = data.model(dbName, tbName, tempno);
    } else if (action == "select") {
        var where = request.param("where");
        var order = request.param("order");
        b_json = data.select(dbName, tbName, where, order);
    } else if (action == "paging") {
        var where = request.param("where");
        var order = request.param("order");
        var pageSize = request.param("pageSize");
        var pageNo = request.param("pageNo");
        b_json = data.paging(dbName, tbName, where, order, pageSize, pageNo);
    } else if (action == "query") {
        var sql = request.param("sql");
        b_json = data.query(dbName, tbName, sql);
    } else if (action == "row") {
        var keyid = request.param("keyid");
        var keyval = request.param("keyval");
        b_json = data.row(dbName, tbName, keyid, keyval);
    } else if (action == "foreignkey") {
        var json_ = request.param("json");
        b_json = data.foreignkey(json_);
    } else if (action == "stock_daily") {
        var ts_code = request.param("ts_code");
        var main_json = data.query(dbName, "select symbol,name,area,industry,list_date FROM stock_list where ts_code='" + ts_code + "'");
        if (main_json.error == 0 && main_json.rows.length > 0) {
            var from_date = new Date();
            from_date.setDate(from_date.getDate() - 360);
            from_date = formatYYYYMMDD(from_date);
            try {
                res = http.get("http://127.0.0.1:8080/api/daily?ts_code=" + ts_code + "&start_date=" + from_date);
                if (res.error == 0 && res.data.length > 0) {
                    b_json = {
                        error: 0,
                        info: "ok",
                        rows: res.data
                    };

                    res = http.get("http://127.0.0.1:8080/api/moneyflow?ts_code=" + ts_code + "&start_date=" + from_date);
                    if (res.error == 0 && res.data.length > 0) {
                        b_json.rows_2 = res.data;
                    }
                }
                
                if (res.error != 0) {
                    b_json = data.query(dbName, "select ts_code,trade_date,open,high,low,close,pre_close,change,pct_chg,vol,amount from stock_daily where ts_code='" + ts_code + "' order by trade_date");
                    b_json.rows_2 =[];
                }
            } catch (e) {
                b_json = data.query(dbName, "select ts_code,trade_date,open,high,low,close,pre_close,change,pct_chg,vol,amount from stock_daily where ts_code='" + ts_code + "' order by trade_date");
                b_json.rows_2 =[];
            }

            b_json.ts_code = ts_code;
            b_json.symbol = main_json.rows[0].symbol;
            b_json.name = main_json.rows[0].name;
            b_json.area = main_json.rows[0].area;
            b_json.industry = main_json.rows[0].industry;
        } else {
            b_json = main_json;
        }
    } else {
        b_json = {
            error: 99,
            info: action + "操作不存在。"
        };
    }
    response.write(JSON.stringify(b_json));

    // yyyyMMdd 转 Date
    function parseYYYYMMDD(dateString) {
        // 验证输入格式是否为8位纯数字
        if (typeof dateString !== 'string' || dateString.length !== 8 || !/^\d+$/.test(dateString)) {
            return new Date(NaN); // 返回无效日期
        }

        // 提取年月日并转为整数
        const year = parseInt(dateString.substring(0, 4), 10);
        const month = parseInt(dateString.substring(4, 6), 10) - 1; // 月份需减1（0-11）
        const day = parseInt(dateString.substring(6, 8), 10);

        // 创建日期对象
        const date = new Date(year, month, day);

        return date;
    }

    // 格式化为 yyyyMMdd（可选）
    function formatYYYYMMDD(date) {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}${month}${day}`;
    }
</script>